1   //==============================================================================
2   // file :       CommandDispatcher.java
3   // project:     Front View Control
4   //
5   // last change: date:       $Date: 2003/09/10 09:13:57 $
6   //              by:         $Author: bitiboy $
7   //              revision:   $Revision: 1.1 $
8   //------------------------------------------------------------------------------
9   // copyright:   GNU GPL Software License (see class documentation)
10  //==============================================================================
11  package com.justhis.control;
12  
13  
14  /*
15   * $Id: CommandDispatcher.java,v 1.1 2003/09/10 09:13:57 bitiboy Exp $
16   *
17   * Copyright 2003 Acai Software All Rights Reserved.
18   *
19   * This file CommandDispatcher.java is part of the Front View Control.
20  
21   * The Front View Control is free software; you can redistribute it and/or modify
22   * it under the terms of the GNU General Public License as published by
23   * the Free Software Foundation; either version 2 of the License, or
24   * (at your option) any later version.
25  
26   * The Front View Control is distributed in the hope that it will be useful,
27   * but WITHOUT ANY WARRANTY; without even the implied warranty of
28   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29   * GNU General Public License for more details.
30  
31   * You should have received a copy of the GNU General Public License
32   * along with the Front View Control; if not, write to the Free Software
33   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
34  
35   * http://www.justhis.com
36   * CONTACT: email = superaxis@sohu.com webmaster@justhis.com
37   */
38  import com.justhis.util.XParameters;
39  import com.justhis.util.exception.JavaClassRefectException;
40  import com.justhis.util.exception.LogicException;
41  import com.justhis.util.exception.PropertiesFileReadException;
42  import com.justhis.util.exception.UtilException;
43  
44  import java.sql.SQLException;
45  
46  import java.util.Enumeration;
47  import java.util.Properties;
48  
49  import javax.servlet.ServletConfig;
50  import javax.servlet.ServletException;
51  import javax.servlet.http.HttpServletRequest;
52  import javax.servlet.http.HttpServletResponse;
53  
54  
55  /***
56   * Instances turn a request into a command, which may then be executed.
57   *
58   * @author <a href="http://blog.ejb.cn">acai</a>
59   * @version $Revision: 1.1 $
60   */
61  public class CommandDispatcher {
62      //~ Static fields/initializers ---------------------------------------------
63  
64      /*** The name of the servlet parameter specifying the command or action. */
65      private static final java.lang.String ACTION_PARAMETER_NAME = "action";
66  
67      //fix me how to get it from a config file.
68  
69      /*** page suffix in the url */
70      private static final java.lang.String PAGE_SUFFIX = ".news";
71  
72      //~ Instance fields --------------------------------------------------------
73  
74      /*** servlet config */
75      private ServletConfig config;
76  
77      /*** The servlet request. */
78      private javax.servlet.http.HttpServletRequest request;
79  
80      /*** The servlet response. */
81      private javax.servlet.http.HttpServletResponse response;
82  
83      /*** The properties associated with the request. */
84      private java.util.Properties properties;
85  
86      //~ Constructors -----------------------------------------------------------
87  
88      /***
89       * Create a new instance of the dispatcher containing the parameters from
90       * the servlet request.
91       *
92       * @param request the servlet request
93       * @param response the servlet response
94       */
95      public CommandDispatcher(ServletConfig config, HttpServletRequest request,
96                               HttpServletResponse response
97                              ) {
98          this.request = request;
99          this.response = response;
100         this.config = config;
101 
102         this.properties = extractProperties(request);
103     }
104 
105     //~ Methods ----------------------------------------------------------------
106 
107     /***
108      * Get the servlet request properties.
109      *
110      * @return the properties for the servlet request
111      */
112     public Properties getProperties() {
113         return properties;
114     }
115 
116     /***
117      * Get the servlet request.
118      *
119      * @return the servlet request
120      */
121     public HttpServletRequest getRequest() {
122         return request;
123     }
124 
125     /***
126      * Get the servlet response.
127      *
128      * @return the servlet response
129      */
130     public HttpServletResponse getResponse() {
131         return response;
132     }
133 
134     /***
135      * get the Servlet config
136      *
137      * @return the servlet config
138      */
139     public ServletConfig getServletConfig() {
140         return this.config;
141     }
142 
143     /***
144      * Get the command specified by the servlet request.
145      *
146      * @return the command specified by the servlet request
147      */
148     public Command getCommand()
149                        throws UtilException, PropertiesFileReadException, 
150                               JavaClassRefectException {
151         String param = getProperties().getProperty(ACTION_PARAMETER_NAME);
152 
153         if (param == null) {
154             if (request.getServletPath().endsWith(PAGE_SUFFIX)) {
155                 return ViewHelper.getCommand("Go");
156             } else {
157                 return ViewHelper.getCommand("index");
158             }
159         }
160 
161         return ViewHelper.getCommand(param);
162     }
163 
164     /***
165      * @see Command#execute()
166      */
167     public String executeCommand()
168                           throws JavaClassRefectException, LogicException, 
169                                  SQLException, ServletException, UtilException {
170         Command command = null;
171 
172         try {
173             command = getCommand();
174         } catch (PropertiesFileReadException e) {
175             throw new UtilException(e);
176         } catch (JavaClassRefectException e) {
177             throw new UtilException(e.getMessage(), e);
178         }
179 
180         if (command == null) {
181             return null;
182         }
183 
184         command.init(this);
185 
186         return command.execute();
187     }
188 
189     /***
190      * Translates servlet request properties to java properties.
191      *
192      * @param the servlet request object
193      *
194      * @return the servlet request properties
195      */
196     private Properties extractProperties(HttpServletRequest servletrequest) {
197         properties = new XParameters();
198 
199         Enumeration enumeration = servletrequest.getParameterNames();
200 
201         while (enumeration.hasMoreElements()) {
202             String name = (String) enumeration.nextElement();
203             String value = servletrequest.getParameterValues(name)[0];
204             System.out.println(name + " 's value: " + value);
205             properties.setProperty(name, value);
206         }
207 
208         properties.setProperty("SYSTEM_OID",
209                                (String) request.getAttribute("SYSTEM_OID")
210                               );
211 
212         return properties;
213     }
214 
215     //fixme to get sytem's page suffix. use jibx,but..
216 
217     /*
218     private SystemConfig getSystemConfig() throws UtilException{
219             try {
220 
221                         return (SystemConfig) JibxUtil.unmarshallDocument(SystemConfig.class,config.getServletContext().getRealPath("./")+"/config/SystemConfig.xml");
222                 } catch (JiBXException e) {
223                         throw new UtilException(e.getMessage(),e);
224                 }
225     }*/
226 }
227 
228 
229 /*
230  * $Log: CommandDispatcher.java,v $
231  * Revision 1.1  2003/09/10 09:13:57  bitiboy
232  * *** empty log message ***
233  *
234  *
235 */
This page was automatically generated by Maven